package com.tonglin.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tonglin.commonutils.R;
import com.tonglin.eduservice.entity.EduCourse;
import com.tonglin.eduservice.entity.EduTeacher;
import com.tonglin.eduservice.entity.vo.CourseInfoVo;
import com.tonglin.eduservice.entity.vo.CoursePublishVo;
import com.tonglin.eduservice.entity.vo.CourseQuery;
import com.tonglin.eduservice.entity.vo.TeacherQuery;
import com.tonglin.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author tonglin
 * @since 2021-05-10
 */
@Api(description = "添加课程")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据课程id返回课程信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("1");  //设置发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //分页显示课程
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current, @PathVariable long limit) {
//        //创建page对象
//        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
//
//        //调用方法实现分页
//        //调用方法，底层封装，把分页所有数据封装到pageTeacher对象里面
//        teacherService.page(pageTeacher,null);
//
//        long total = pageTeacher.getTotal();
//        List<EduTeacher> records = pageTeacher.getRecords();  //数据list集合
//
//        return R.ok().data("total",total).data("rows",records);
        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current, limit);

        //调用方法实现分页
        //调用方法，底层封装，把分页所有数据封装到pageCourse对象里面
        courseService.page(pageCourse, null);

        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords(); //数据list集合

        return R.ok().data("total", total).data("rows", records);

    }

    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        System.out.println(list);
        return R.ok().data("list", list);
    }

    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CourseQuery courseQuery){
         //创建page对象
        System.out.println("-----"+current);
       Page<EduCourse> pageCourse = new Page<>(current,limit);
        //构建条件
       QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
       String title = courseQuery.getTitle();
       String status = courseQuery.getStatus();
       if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
         if (!StringUtils.isEmpty(status)){
             wrapper.like("status",status);
         }
        wrapper.orderByDesc("gmt_create");
         //调用方法实现分页
         //调用方法，底层封装，把页面所有数据封装到pageTeacher对象里面
         courseService.page(pageCourse,wrapper);
         long total = pageCourse.getTotal();
         List<EduCourse> records = pageCourse.getRecords();  //数据list集合
         return R.ok().data("total",total).data("rows",records);
     }

     @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        courseService.removeCourseById(courseId);
        return R.ok();

     }


}

